//
//  FileCenter.m
//  first app
//
//  Created by 林 小明 on 14-7-16.
//
//

#import "FileCenter.h"

@implementation FileCenter

+ (NSString*)GetDocumentsDirPath
{
    NSString *documentsDirectory= [NSHomeDirectory()
                                   stringByAppendingPathComponent:@"Documents"];
    return documentsDirectory;
}

+ (long long)SaveToFile:(NSString *)dirName fileName:(NSString *)fileName fileData:(NSData *)data fileOffset:(unsigned long long)fileOffset
{
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *dirPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, dirName];
    
    NSString *filePath= [dirPath stringByAppendingPathComponent:fileName];
    
    BOOL isDir;
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    if( ![fileMgr fileExistsAtPath:filePath isDirectory:&isDir] )
    {
        [fileMgr createFileAtPath:filePath contents:nil attributes:nil];
    }
    
    NSFileHandle *outFile;
    
    outFile = [NSFileHandle fileHandleForWritingAtPath:filePath];
    
    if(outFile == nil)
    {
        NSLog(@"fileHandleForWritingAtPath fail");
        return -1;
    }
    
    [outFile seekToFileOffset:fileOffset];
    
    [outFile writeData:data];
    
    long long offset = [outFile offsetInFile];
    
    [outFile closeFile];

    //NSLog(@"SaveToFile:[%@]",filePath);
    
    return offset;
}

+ (BOOL)SaveToStringFile:(NSString *)dirName fileName:(NSString *)fileName fileData:(NSString *)content
{
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *dirPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, dirName];
    
    NSString *filePath= [dirPath stringByAppendingPathComponent:fileName];
    
    BOOL isDir;
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    if( ![fileMgr fileExistsAtPath:filePath isDirectory:&isDir] )
    {
        [fileMgr createFileAtPath:filePath contents:nil attributes:nil];
    }
    
    NSLog(@"SaveToFile:[%@]",filePath);
    
    return [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

+ (NSString *)ReadStringFile:(NSString *)dirName fileName:(NSString *)fileName
{
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *dirPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, dirName];
    
    NSString *filePath= [dirPath stringByAppendingPathComponent:fileName];
    
    BOOL isDir;
    
    //NSLog(@"ReadFile:[%@]",filePath);
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    if( ![fileMgr fileExistsAtPath:filePath isDirectory:&isDir] )
    {
        return nil;
    }
    
    return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
}

+ (NSData *)ReadFile:(NSString *)dirName fileName:(NSString *)fileName
{
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *dirPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, dirName];
    
    NSString *filePath= [dirPath stringByAppendingPathComponent:fileName];
    
    BOOL isDir;
    
    //NSLog(@"ReadFile:[%@]",filePath);
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    if( ![fileMgr fileExistsAtPath:filePath isDirectory:&isDir] )
    {
        return nil;
    }
    
    return [NSData dataWithContentsOfFile:filePath];
}

+ (void)CreateDocumentsDirectory:(NSString *)dirName
{
    //对于错误信息
    NSError *error;
    
    // 创建文件管理器
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    //指向文件目录
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *dirPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, dirName];
    
    BOOL isDir;
    if( ![fileMgr fileExistsAtPath:dirPath isDirectory:&isDir] )
    {
        //创建一个目录
        [fileMgr createDirectoryAtPath:dirPath  withIntermediateDirectories:YES
            attributes:nil error:&error];
    }
}

+ (BOOL)CopyFile:(NSString *)src_dir_name srcFileName:(NSString *)src_file_name
destDirName:(NSString *)dest_dir_name destFileName:(NSString *)dest_file_name
{
    NSError *error;
    
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *src_path = [NSString stringWithFormat:@"%@/%@/%@", documentsDirectory, src_dir_name, src_file_name];
    
    NSString *dest_dir_path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, dest_dir_name];
    
    BOOL isDir;
    NSFileManager *fm = [NSFileManager defaultManager];
    if( [fm fileExistsAtPath:src_path isDirectory:&isDir] && !isDir )
    {
        if( !([fm fileExistsAtPath:dest_dir_path isDirectory:&isDir] && isDir) )
        {
            //创建一个目录
            [fm createDirectoryAtPath:dest_dir_path withIntermediateDirectories:YES
                           attributes:nil error:&error];
        }
        
        NSString *dest_path = [NSString stringWithFormat:@"%@/%@", dest_dir_path, dest_file_name];
        
        NSData *mainBundleFile = [NSData dataWithContentsOfFile:src_path];
        [fm createFileAtPath:dest_path contents:mainBundleFile attributes:nil];
        
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)IsFileExistInDocuments:(NSString *)dirName fileName:(NSString *)fileName
{
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    //指向文件目录
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *dirPath = [NSString stringWithFormat:@"%@/%@/%@", documentsDirectory, dirName, fileName];
    
    BOOL isDir;
    if( ![fileMgr fileExistsAtPath:dirPath isDirectory:&isDir] )
    {
        return NO;
    }
    
    return YES;
}

+ (void)DeleteFile:(NSString *)dirName fileName:(NSString *)fileName
{
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@/%@", documentsDirectory, dirName, fileName];
    
    //NSLog(@"DeleteFile:[%@]",filePath);
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    [fileMgr removeItemAtPath:filePath error:nil];
}

+ (NSDictionary*)GetDocumentsFileAttribute:(NSString *)dirName fileName:(NSString *)fileName
{
    NSFileManager *fileMgr = [NSFileManager defaultManager];

    //指向文件目录
    NSString *documentsDirectory =[self GetDocumentsDirPath];
    
    NSString *dirPath = [NSString stringWithFormat:@"%@/%@/%@", documentsDirectory, dirName, fileName];
    
    NSError *error = nil;
    
    NSDictionary* dictFile = [fileMgr attributesOfItemAtPath:dirPath error:&error];
    if (error)
    {
        NSLog(@"getfilesize error: %@", error);
        return nil;
    }
    
    return dictFile;
}

@end
